//
//  JMainTabBar.swift
//  JMicroBlog
//
//  Created by Jason on 18/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit

class JMainTabBar: UITabBar {
    
    // MARK: - 撰写按钮的添加
    
    // 重写布局子视图的方法
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 按钮的总数
        let btnCount = 5
        let w = self.bounds.size.width / CGFloat(btnCount)
        let h = self.bounds.size.height
        
        // let frame = CGRectMake(0,0,w,h)   // Teacher
        
        var index : CGFloat = 0
        // 遍历子数组
        for v in subviews as! [UIView] {
            
            // 是UIBarButtonItem对象
            if v is UIControl && !(v is UIButton) {
                // 按钮的大小固定
                v.frame = CGRectMake(index * w, 0, w, h)
                
                // v.frame = CGRectOffset(frame, index * w, 0) // Teacher
                
                index += index == 1 ? 2 : 1
            }
        }
        
        // 添加撰写按钮
        composedBtn.frame = CGRectMake(2 * w, 0, w, h)
        
        // 开始使用了不太好的方法 设置最左边的frame 然后又设置center = CGMakePoint(self.center.x, h * 0.5)
    }
    
    // 撰写按钮懒加载
    lazy var composedBtn : UIButton = {
       let btn = UIButton()
        
       // 设置撰写按钮的前景与背景图片
       btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
       btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
       btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
       btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
       // 添加子视图到UITabBar中  记得添加self
       self.addSubview(btn)
        
       return btn
    }()
}
